<%@ page contentType="text/html; charset=utf-8" import="com.running.crm.model.Mms,com.running.crm.model.MmsContent,java.util.*,com.running.util.StringUtils,com.running.util.FileOperator"%><%
Mms mms = (Mms)request.getAttribute("mms");
List<Map> smilContents = (List<Map>)request.getAttribute("smilContents");
String rootDir = config.getServletContext().getRealPath("/").replaceAll("\\\\","/");	//根目录
if (rootDir.endsWith("/")) {
	rootDir = rootDir.substring(0,rootDir.length()-1);
}
String mmsDstNum = StringUtils.varFormat(request.getParameter("phoneNo"));
%>
<html>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<script language="javascript">
var returnUrl;
var tmpImgFileDir = new Array();
var tmpImgFileName = new Array();
var tmpImgFileSize = new Array();
//var tmpVoiFileDir = new Array();
//var tmpVoiFileName = new Array();
//var tmpVoiFileSize = new Array();
var tmpMmsText = new Array();
var tmpFrameTime = new Array();
function PopDialog(fileDirObj,fileNameObj,fileSizeObj,disObj){
	var url ="file.htm";
	returnUrl = showModalDialog(url, window, 'dialogWidth:400px;dialogHeight:220px;help:no;scroll:no;status:no');
	if(returnUrl){
		fileDirObj.value = returnUrl[0]
		fileNameObj.value = returnUrl[1];
		fileSizeObj.value = returnUrl[2];
		if(disObj){
			//显示在页面上的img路径,也就是img的src
			disObj.src = returnUrl[0]+"/"+returnUrl[1];
		}
	}
}

window.onload = function Init(){
	var objMmsContainer = document.getElementById('oFrameSpan');
	loadXMLData2Select(document.forms[0].mmsSidFk,mmsConfig.xml,"mmsSubject","mmsSid","<%=mms.getMmsSid()%>");
	oForm.frameCount.value="<%=smilContents.size()%>";
	loadMmsContent();
	setFrame(oForm.frameCount.value);
    
}

function Choice(){
    var returnValue = showModalDialog("customer.do?method=choice&textColumn=phoneInfo&valueColumn=phoneNo&temp="+Math.random(),document.oForm,"dialogWidth:53em; dialogHeight:42em; status:0");
    if (returnValue != null) {
         var s = returnValue[0].split(/\D/g).length;//以1,1形式为两个字符
		if(s>10000){
		      alert("您导入的手机号码超过10000条，请按批次导入!");
		 }else{
			 if (oForm.phoneNo.value=="") {
				 oForm.phoneNo.value = returnValue[0];
			  } else {
				 oForm.phoneNo.value += ","+returnValue[0];
			 }
			   calculatePhone(oForm.phoneNo);
		 }
        //去除可能重复的手机号码
        //oForm.phoneNo.value = oForm.phoneNo.value.split(',').sort().join(',').replace(/(\d{11},)\1+/g, '$1');
       // calculatePhone(oForm.phoneNo);
    }
}

function calculatePhone(obj) {
	var s = obj.value.split(/\D/g).length;//以1,1形式为两个字符
	if(s>10000){
		   alert("您导入的手机号码超过10000条，请按批次导入!");
	}else{
	 document.getElementById("phoneTip").innerHTML = "共有"+s+"个电话号码";
	 document.oForm.phoneNo.value = obj.value.split(/\D/g).sort().join('\r\n').replace(/(\d{11}\r\n)\1+/g, '$1');
   }
}
function setFrame(iFrameCount){
	var objMmsContainer = document.getElementById('oFrameSpan');
	if (objMmsContainer) {
		if (objMmsContainer.children)
			oldFrameCount = objMmsContainer.children.length;
		for (var i=1;i<=oldFrameCount;i++) {
			tmpImgFileDir[i-1] = document.getElementByName("imgFileDir"+i).value;
			tmpImgFileName[i-1] = document.getElementByName("imgFileName"+i).value;
			tmpImgFileSize[i-1] = document.getElementByName("imgFileSize"+i).value;
			tmpMmsText[i-1] = document.getElementByName("mmsText"+i).value;
			tmpFrameTime[i-1] = document.getElementByName("frameTime"+i).value;
		}
	}
	var sFrame = "";
	for(var i=1;i<=parseInt(iFrameCount);i++){
		if (tmpImgFileDir.length>=i) {
			sFrame += buildMmsFrame(i,tmpImgFileDir[i-1],tmpImgFileName[i-1],tmpImgFileSize[i-1],tmpMmsText[i-1],tmpFrameTime[i-1],"N");
		} else {//一切新增
			sFrame += buildMmsFrame(i,"","","","","","N");
		}
	}
	document.getElementByName("oFrameSpan").innerHTML = sFrame;
}

function gotoMmsConfig(configId){
	var reg=/\r/g;
    if(configId){
        location = "mms.do?method=append&mmsSid="+configId+"&phoneNo="+oForm.phoneNo.value.replace(reg,",");
    }
}


function loadMmsContent() {
<%
int i = 0;
for(Map smilContent:smilContents){
	out.println("tmpImgFileDir["+i+"]=''");
	out.println("tmpImgFileName["+i+"]=''");
	out.println("tmpImgFileSize["+i+"]=''");
	out.println("tmpMmsText["+i+"]=''");
	out.println("tmpFrameTime["+i+"]='10'");
	for (MmsContent mmsContent : mms.getMmsContents()) {
		if (StringUtils.varFormat(String.valueOf(smilContent.get("imgFileName"))).equals(mmsContent.getFileName())) {
			out.println("tmpImgFileDir["+i+"]='"+mmsContent.getFileDir()+"'");
			out.println("tmpImgFileName["+i+"]='"+mmsContent.getFileName()+"'");
			out.println("tmpImgFileSize["+i+"]='"+mmsContent.getFileSize()+"'");
		}
		if (StringUtils.varFormat(String.valueOf(smilContent.get("txtFileName"))).equals(mmsContent.getFileName())) {
			if (FileOperator.exists(rootDir + "/" + mmsContent.getFileDir() + "/" + mmsContent.getFileName())) {
				out.println("tmpMmsText["+i+"]='"+FileOperator.read(rootDir + "/" + mmsContent.getFileDir() + "/" + mmsContent.getFileName()).replace("\r\n","\\r\\n").replace("'","\\'")+"'");
			}
		}
	}
	if (!StringUtils.varFormat(String.valueOf(smilContent.get("frameTime"))).equals("")) {
		out.println("tmpFrameTime["+i+"]='"+StringUtils.varFormat(String.valueOf(smilContent.get("frameTime")))+"'");
	}
	i++;
}	
%>
}

function beforeSubmit(oForm){
	var phoneNo = oForm.phoneNo.value.replace(/\D/g,"\r\n");
	//去除可能重复的手机号码
	oForm.phoneNo.value = phoneNo.split(/\D/g).sort().join('\r\n').replace(/(\d{11}\r\n)\1+/g, '$1');
	if (ValidateForm(oForm)) {
		return true;
	} else {
		return false;
	}
}
function clearPhones(){
	    document.oForm.phoneNo.value="";
	    document.getElementById("phoneTip").innerHTML = "共有0个电话号码";
	}
</script>
</head>
<body>
<xml id="mmsConfig" src="mmsConfig.do?method=listXML"></xml>
<form name="oForm" onsubmit="return beforeSubmit(this)" method="POST" action="mms.do?method=save">
<input type="hidden" name="frameCount" value="1"/>
<table id="FormTable">
<caption>向您的客户发送彩信(目前只支持向省内移动手机号码发送)</caption>
<tr><td>
	<font class="FieldItem">手机号码<font class="NotNull">(*必填项)<span id="phoneTip">共有0个电话号码</span></font></font>&nbsp;&nbsp;<input type="button"  class="DefaultButton" value="导入客户号码" onclick="Choice()"/>
	&nbsp;<input type="button"  class="DefaultButton" value="清除客户号码" onclick="clearPhones()" /><br/>
	<textarea name="phoneNo" _cnName="接收彩信手机号" _dataType="String" _dataLength="129998" _dataEmpty="false" style="height:80px"  readonly><%=mmsDstNum%></textarea>
</td></tr>
<tr>
	<td><font class="FieldItem">选择彩信</font>&nbsp;<a href="mmsConfig.do?method=append">新增彩信</a><br/>
        <select name="mmsSidFk" onchange="gotoMmsConfig(this.value)"></select>
    </td>
</tr>
<tr><td>
	<fieldset>
		<legend>彩信预览</legend>
		<span id="oFrameSpan"></span>
	</fieldset>		
</td></tr>
<tr><td><font class="FieldItem">发送时间</font><br/><input name="mmsAtTime" _dataType="string" _cnName="发送时间" _dataLength="255" class="defaultCalendar"></td></tr>
<tr><td><HR/></td></tr>
<tr><td><input type="submit" name="btnSubmit" value=" 发 送 " class="DefaultButton"/>&nbsp;<input type="button" name="btnCancel" value=" 取 消 " class="DefaultBtnCancel"/></td></tr>
</table>
</form>
</body>
</html>